var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var mallUrl = require("url/mall");

var shopId = null;
var shopName = null;
var shopNameNew = "";
//获取上一个页面传入的参数
page.on("loaded", function(){
	var params = page.getData();
	shopId = params.id;
	shopName = params.name;
	ui(addShopName+".textField").text = shopName;
});

//返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "修改店铺名称";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage();
});

//保存
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "保存";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	shopNameNew = ui(addShopName+".textField").text;
	if(shopNameNew.trim() == ""){
		nf.toast("请输入修改信息");
		return;
	}
	if (shopNameNew.length > 16) {
		nf.toast("名称不能超过16个字符");
		return;
	}
	submit();
});

function submit(){
	var paramData = {
		id:shopId,
		name:shopNameNew
	};
	http.post(mallUrl.url.shop.updateName, paramData, function(data) {
		app.fire("fireShopUpdate",{"name":shopNameNew});
		app.fire("fireShopFromUpdate",{"name":shopNameNew});
		app.closePage();
	});
};



var linearLayout_main = ui("do_LinearLayout_main");

/** -------------------add UI start ---------------------------**/
var addShopName = linearLayout_main.add("shopName", "source://view/reuse/textField.ui",0,0);
var shopName = ui("shopName");
shopName.bindData(hashdata);
hashdata.addData({
    "textFieldHint":"请输入店铺名称"
});
shopName.refreshData();

var addLine = linearLayout_main.add("line", "source://view/reuse/form/line.ui",0,0);
ui("line").refreshData();
/** -------------------add UI end ---------------------------**/
